import BuffUtil from '@/BuffUtil';
import UnitSpellEvent from '@/event/UnitSpellEvent';
import UnitItemEvent from '@/event/UnitItemEvent';
import ArrayUtil from '@/ArrayUtil';
import BaseUtil from '@/BaseUtil';
import SelectUtil from '@/SelectUtil';

import RandomUtil from '@/RandomUtil';
import ItemUtil from '@/ItemUtil';
import EffectCommon from 'code/Core/Common/EffectCommon';
export default class Item_辉耀 {
    static id = 'shhn';
    static 名称: string = '辉耀';
    static 描述: string = '你的攻击有10%的机率丢失';
    static 图标: string = 'ReplaceableTextures\\CommandButtons\\BTNTransmute.blp';
    static buffId: string = 'buff_item_huiyao';
    static 光环提供者数组: unit[] = [];
    static buffType: AppBuffType = {
        id: 'buff_item_huiyao',
        name: Item_辉耀.名称,
        ubertip: Item_辉耀.描述,
        art: Item_辉耀.图标,
        class: '光环',
        dur: 2,
        isDebuff: true,
    };
    static 添加技能(目标: unit) {
        UnitAddAbility(目标, Item_辉耀.id);
    }
    static 释放(x: number, y: number, 来源?: unit, 目标?: unit, d?: any) {}
    constructor() {
        BuffUtil.registerBuffType(Item_辉耀.buffType);
        se.onUnitPickupItem(Item_辉耀.获取物品);
        se.onUnitDropItem(Item_辉耀.失去物品);
        se.onUnitPawnItem(Item_辉耀.失去物品);
        se.onUnitSellItem(Item_辉耀.失去物品);
        BaseUtil.onTimer(2, Item_辉耀.计时器函数);
        EventManager.onBeforApplyAttack(Item_辉耀.处理攻击函数);
    }
    static 处理攻击函数(d: DamageEventData) {
        if (!BuffUtil.hasUnitBuff(d.source, Item_辉耀.buffId)) return;
        let r = RandomUtil.randomPercent();
        if (r < 0.1) {
            d.hasDogged = true;
        }
    }
    static 计时器函数() {
        for (let u of Item_辉耀.光环提供者数组) {
            if (!ItemUtil.isUnitHasItem(u, Item_辉耀.id)) continue;
            let es = SelectUtil.getEnemyUnitsInRange(u, 650);
            for (let e of es) {
                DamageSystemInstance.applyLightDamage(e, u, 65);
                let ef = AddSpecialEffectTarget('Abilities\\Spells\\Items\\HealingSalve\\HealingSalveTarget.mdl', e, 'origin');
                EffectCommon.delyRemove(ef, 1);
                BuffUtil.addBuff(e, Item_辉耀.buffId);
            }
        }
        return true;
    }
    static 失去物品(d: UnitItemEvent) {
        if (GetItemTypeId(d.manipulatedItem) != FourCC(Item_辉耀.id)) return;
        let u = GetTriggerUnit();
        BaseUtil.runLater(0.1, () => {
            if (ItemUtil.getItemCountOfTypeFromUnit(u, Item_辉耀.id) == 0) ArrayUtil.removeElement(Item_辉耀.光环提供者数组, u);
        });
    }
    static 获取物品(this: void, d: UnitItemEvent) {
        if (GetItemTypeId(d.manipulatedItem) != FourCC(Item_辉耀.id)) return;
        if (!Item_辉耀.光环提供者数组.includes(d.trigUnit)) Item_辉耀.光环提供者数组.push(d.trigUnit);
    }
    static spellEffect(this: void, d: UnitSpellEvent) {}
}
